/**
 * WordGuessGameOne.java
 * 
 * Player class is used by the WordGameTournament class to play the 
 * first word guessing game.
 * 
 * CSCE 155 Spring 2010
 * Assignment 2
 * @author Josh Branchaud
 * @version 1.0, 2/3/2010
 */

// import statements
import java.io.*;
import java.util.*;

public class WordGuessGameOne
{
	// public class constant   
	public static final int MAX_GUESSES = 10;

	// private data members -- variables   
	private String secret;
        private String hint;
	private StringBuffer buffer;
	private int incorrectGuesses;
	private int guesses;
	
	// Adding a bunch of random ints
	private int one = 1;
	private int two = 2;
	private int three = 3;
	private int four = 4;
	
	// remove this later
	private int anotherVariable;

    //*** Constructor(s) ***//

    public WordGuessGameOne()
    {
        this.secret = "";
        this.hint = "";
        this.buffer = new StringBuffer("");
        this.incorrectGuesses = 0;
        this.guesses = 0;
    }

    //*** end of Constructor(s) ***//


    /**
     * playGameOne is a method that will be called by WordGuessTournament in
     * order to execute the methods involved in playing Hang Man.
     * @param secretWord, whoseFirst
     * @return String
     */
    public String playGameOne(String secretWord, int whoseFirst)
    {
        // String to hold the outcome of the game.
        String outcome = "";

        // Store the given secretWord in secret
        this.secret = secretWord;

        // generate the hint based on the given secret
        this.generateHint();

        // Intro message
        System.out.println("Please enter your letter guesses for the following word:");
        System.out.println(this.hint);

        while(this.hint.compareTo(this.secret) != 0 && incorrectGuesses < 10)
        {
            this.obtainGuess();
        }

        // Win/Lose Message depending on outcome.
        if(incorrectGuesses >= 10)
        {
            System.out.println("Exceeded Guess Limit. You lose.");
            outcome = "lose";
        }
        else if(this.hint.compareTo(this.secret) == 0)
        {
            System.out.println("Bingo! You won.");
            outcome = "win";
        }

        return outcome;
    }

    /**
     * obtainGuess will allow the user to enter a letter that they hope will be
     * contained in the secret. If the guess is anything but a letter, then an
     * error message should be displayed and incorrectGuesses will NOT be
     * incremented.
     * @param None
     * @return void
     */
    public void obtainGuess()
    {
        // Get the player's input
        String guess = this.readString();
        guess = guess.toUpperCase();

        if(guess.length() == 1 && Character.isLetter(guess.charAt(0)))
        {
            this.checkGuess(guess);
        }
        else
        {
            System.out.println("Invalid Guess, please use ONLY letters.");
        }
    }

    /**
     * checkGuess is a method that will see if there are any occurences of the
     * given letter in the secret. If there are, those occurences will be
     * revealed in hint and hint will be printed to the console again.
     * @param guess
     * @return void
     */
    public void checkGuess(String guess)
    {
        // counter for correct occurences
        int correctOccur = 0;

        // Convert hint string to a bufferstring
        this.buffer = new StringBuffer(this.hint);

        for(int i = 0; i < this.secret.length(); i++)
        {
            // If the a correct letter is guessed more than once, then break and
            // print hint as it is.
            if(Character.toString(this.secret.charAt(i)).compareTo(guess) == 0
                    && Character.toString(this.hint.charAt(i)).compareTo(guess) == 0)
            {
                correctOccur++;
                break;
            }
            // else if the letter is correctly guessed for the first time, then
            // update the appropriate values of hint.
            else if(Character.toString(this.secret.charAt(i)).compareTo(guess) == 0)
            {
                this.buffer = this.buffer.replace(i, i+1, guess);
                correctOccur++;
            }
            else
            {
                // do nothing, doesn't match.
            }
        }

        if(correctOccur > 0)
        {
            this.guesses++;
        }
        else
        {
            this.guesses++;
            this.incorrectGuesses++;
        }

        // Convert it back to a string
        this.hint = this.buffer.toString();

        // Print the hint again
        System.out.println(this.hint);
    }

    /**
     * generateHint is a method that will basically make a string of hyphens
     * that is the same length as secret.
     * @param None
     * @return void
     */
    private void generateHint()
    {
        // obtain the length of secret
        int len = this.secret.length();

        // make sure hint is a blank String
        this.hint = "";

        // if the length is > 0, then use a for loop to generate the hint.
        if(len > 0)
        {
            for(int i = 0; i < len; i++)
            {
                this.hint = this.hint + "-";
            }
        }
    }


	//*** Getter Methods for Private Variables ***//

    /**
     * getIncorrectGuesses retrieves the value of incorrectGuesses.
     * @param None
     * @return int
     */
    public int getIncorrectGuesses()
    {
        return this.incorrectGuesses;
    }

    //*** end of Getter Methods ***//


    //*** Setter Methods for Private Variables ***//



    //*** end of Setter Methods ***//

    
    /**
     * This method reads in a string and returns that string to the caller of
     * this method.
     */
    private String readString()
    {
        
        String userInput = "";
        Scanner scanner = new Scanner(System.in);
        
        userInput = scanner.nextLine();
        
        return userInput;
        
    }  // end readString

} //end of class WordGuessGameOne
